/**
 * @file app\tasks\mobus\_config_parameter.c.c
 *
 * Copyright (C) 2023
 *
 * _config_channel.c is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Zhuodada
 *
 * @encoding utf-8
 */

/*---------- includes ----------*/
#include "protocol_mobus.h"
#include "storage.h"
/*---------- macro ----------*/
/*---------- type define ----------*/
typedef enum {
    led_mode = 1,
    left_motor_level = 0x1d,
    left_motor_time = 0x1e,
    right_motor_level = 0x1f,
    right_motor_time = 0x20,
} parameters;
/*---------- variable prototype ----------*/
/*---------- function prototype ----------*/
/*---------- variable ----------*/
static struct storage_led_struct* led_parameters = NULL;
static struct storage_pir_struct* pir_parameters = NULL;
static struct storage_key_struct* key_parameters = NULL;
static struct storage_motor_struct* motor_parameters = NULL;
/*---------- function ----------*/
static void __config_parameters_set(mobus_device_t self, uint8_t sub_channel,
    uint8_t start, const uint8_t* pbuf,
    uint8_t numbers) {
    unsigned char parameter_value = 0;
    bool led_once_flag = false;
    bool motor_once_flag = false;
    uint8_t dev_channlx_sta_buffer[4] = { 0 };
    if (numbers == 1) {
        led_parameters = (struct storage_led_struct*)storage_get(STORAGE_AREA_LIGHT_MODE);
        pir_parameters = (struct storage_pir_struct*)storage_get(STORAGE_AREA_PIR);
        key_parameters = (struct storage_key_struct*)storage_get(STORAGE_AREA_KEY);
        assert(key_parameters);
        assert(led_parameters);
        assert(pir_parameters);
        parameter_value = pbuf[0];
        if (start == led_mode) {
            if (parameter_value == pir_bright) {
                pir_parameters->pir_enanle_flag = true;
            }
            else {
                led_parameters->led_mode = parameter_value;
                pir_parameters->pir_enanle_flag = false;
            }
            led_once_flag = true;
            no_storage.led_temp_variable.dev_work_mode = LED_MODE_UPDATA;
            dev_channlx_sta_buffer[0] = 1;
            if (parameter_value == die_bright) {
                led_parameters->assign_bright = 2.5 * _led_value_zero;
                dev_channlx_sta_buffer[1] = _led_value_zero;
            }
            else if (parameter_value == full_bright) {
                led_parameters->assign_bright = 2.5 * _led_value_max;
                dev_channlx_sta_buffer[1] = _led_value_max;
            }
            else if (parameter_value == mid_bright) {
                led_parameters->assign_bright = 2.5 * _led_value_mid;
                dev_channlx_sta_buffer[1] = _led_value_mid;
            }
            else if (parameter_value == low_bright) {
                led_parameters->assign_bright = 2.5 * _led_value_min;
                dev_channlx_sta_buffer[1] = _led_value_min;
            }
            dev_channlx_sta_buffer[2] = key_parameters->dis_en_flag & 0x0f;
            dev_channlx_sta_buffer[3] = (key_parameters->dis_en_flag >> 8) & 0x0f;
            protocol_mobus_dev_report(MOBUS_FRAME_TYPE_UPLOAD_STATUS, sub_channel,
                dev_channlx_sta_buffer, 4);
        }
        else if (start == left_motor_level) {
            motor_once_flag = true;
        }
        else if (start == right_motor_level) {
            motor_once_flag = true;
        }
        else if (start == left_motor_time) {
            motor_once_flag = true;
        }
        else if (start == right_motor_time) {
            motor_once_flag = true;
        }
    }
    else if (numbers > 1) {
    }
    if (led_once_flag == true) {
        led_once_flag = false;
        storage_set(STORAGE_AREA_LIGHT_MODE, led_parameters,
            sizeof(struct storage_led_struct));
    }
    if (motor_once_flag == true) {
        motor_once_flag = false;
        storage_set(STORAGE_AREA_MOTOR, motor_parameters,
            sizeof(struct storage_motor_struct));
    }
}

static void __config_parameters_report(mobus_device_t self, uint8_t sub_channel,
    uint8_t start, uint8_t* pbuf,
    uint8_t numbers) {
    led_parameters =
        (struct storage_led_struct*)storage_get(STORAGE_AREA_LIGHT_MODE);
    pir_parameters = (struct storage_pir_struct*)storage_get(STORAGE_AREA_PIR);
    motor_parameters =
        (struct storage_motor_struct*)storage_get(STORAGE_AREA_MOTOR);
    assert(motor_parameters);
    assert(led_parameters);
    assert(pir_parameters);
    if (sub_channel == 0) {
        if (numbers == 1) {
            if (start == led_mode)
                pbuf[0] = led_parameters->led_mode;
            else if (start == left_motor_level)
                pbuf[0] = 0XFF; // motor_parameters->left_motor_lever;
            else if (start == right_motor_level)
                pbuf[0] = 0XFF; // motor_parameters->right_motor_lever;
            else if (start == left_motor_time)
                pbuf[0] = 0XFF; // motor_parameters->left_motor_time;
            else if (start == right_motor_time)
                pbuf[0] = 0XFF; // motor_parameters->right_motor_time;
            else
                pbuf[0] = 0xff;
        }
        else if (numbers > 1) {
        }
    }
}
